var  statusToColors= [{
		// 不执行的操作0
		locked: 1,
		background: "grey",
		// color: "grey",
		color: "#BFBFBFFF",
		borderWidth: 1,
		bkType: 2, // 0-没有渐变 1-线性渐变 // 2-发散渐变
		textColor: "#fff",
		toArrowColor: "grey",
		fromArrowColor: "grey",
		gradientFromColor: "#80808033",
		gradientToColor: "#80808099",
	},
	{
		// 已完成1
		locked: 1,
		background: "green",
		// color: "green",
		color: "#56FD7EFF",
		textColor: "#fff",
		bkType: 2,
		toArrowColor: "green",
		fromArrowColor: "green",
		gradientFromColor: "#29BE2933",
		gradientToColor: "#29BE2999",
	},
	{
		// 后续操作2
		locked: 1,
		background: "yellow",
		// color: "yellow",
		color: "#FEEF45FF",
		textColor: "#fff",
		bkType: 2,
		toArrowColor: "yellow",
		fromArrowColor: "yellow",
		gradientFromColor: "#EDB10E33",
		gradientToColor: "#EDB10E99",
	},
	{
		// 当前节点3
		locked: 1,
		background: "orange",
		bkType: 2,
		// color: "orange",
		color: "orange",
		textColor: "#000",
		toArrowColor: "orange",
		fromArrowColor: "orange",
	},
];
allNodes = {};
nodesOption = {}; // 备用流程节点（暂时不需要）
linesOption = []; // 备用流程连线
linkLinesOption = []; // 获取所有的连接线


// 获取节点数据，区分数组以适配新版乐吾乐
function getNodeData(node) {
	let data = node;
	if (Array.isArray(node)) {
		data = node[0];
	}
	return data;
};
// 获取前序节点
function getPrevNodeObj(canvasObj, currentNodeId, type = "lcline") {
	let relativeLines = getPrevLineObj(canvasObj, currentNodeId, type);
	let prevNode = null;
	// 获取所有相关线的来源
	relativeLines.forEach((line) => {
		let srcId = line.from.id;
		if (this.allNodes[srcId] && this.allNodes[srcId].status == 1) {
			prevNode = canvasObj.find(srcId);
		}
	});
	if (!prevNode) {
		console.error("未找到前序节点");
	}
	return prevNode;
};
// 获取中继节点之间的连接线，并改变连线状态
function getLineStatusNeededChanged(canvasObj, nodeStatusNeededChanged) {
	let nodeStatusNeededChangedObj = {};
	nodeStatusNeededChanged.forEach((node) => {
		nodeStatusNeededChangedObj[node.id] = node;
	});

	let lines = canvasObj.find("lcline");
	let linesStatusNeededChanged = lines.filter((line) => {
		return (
			line.to &&
			line.from &&
			line.to.id &&
			line.from.id &&
			nodeStatusNeededChangedObj[line.to.id] &&
			nodeStatusNeededChangedObj[line.from.id]
		);
	});
	linesStatusNeededChanged &&
		linesStatusNeededChanged.forEach((line) => {
			this.allNodes[line.id] = {
				id: line.id,
				status: 1, // 1-已完成 2-进行中 0-未操作
				type: "line",
			};
		});
};
// 获取所有需要改变的节点
function getNodeStatusNeededChanged(canvasObj, finishNodes, currentNode, armcode) {
	let nodeStatusNeededChanged = [];
	if (!finishNodes && !currentNode) {
		console.error("当前流程图中没有此报警code：" + armcode);
	} else {
		if (finishNodes) {
			if (Array.isArray(finishNodes)) {
				finishNodes.forEach((node) => nodeStatusNeededChanged.push(node));
			} else {
				nodeStatusNeededChanged.push(finishNodes);
			}
		}
		if (currentNode) {
			if (Array.isArray(currentNode)) {
				currentNode.forEach((node) => nodeStatusNeededChanged.push(node));
			} else {
				nodeStatusNeededChanged.push(currentNode);
			}
		}
		// 处理初始节点，如果需要改变颜色的节点数>0 将初始节点置为已完成
		if (nodeStatusNeededChanged.length > 0) {
			let {
				startNode
			} = this.initStartNode(canvasObj, 1); // 初始节点为已完成
			if (startNode) {
				if (Array.isArray(startNode)) {
					startNode.forEach((node) => nodeStatusNeededChanged.push(node));
				} else {
					nodeStatusNeededChanged.push(startNode);
				}
			}
		} else {
			this.initStartNode(canvasObj);
		}
	}
	return nodeStatusNeededChanged;
};
// 获取标识 code-m， 代表流程中继节点
function getNodeByCodeM(canvasObj, armcode) {
	let finishNodes = getCurrentNode(canvasObj, armcode + "-m");
	if (finishNodes) {
		if (!Array.isArray(finishNodes)) {
			finishNodes = [finishNodes];
		}
	}
	finishNodes &&
		finishNodes.forEach((node) => {
			this.allNodes[node.id] = {
				id: node.id,
				status: 1,
				type: "node",
			};
		});
	return finishNodes;
};
// 获取标识 code-e， 代表流程结束节点
function getNodeByCodeE(canvasObj, armcode) {
	let currentNode = getCurrentNode(canvasObj, armcode + "-e");
	// 改变定位到的最后一个节点的状态和关联线的状态
	if (currentNode) {
		let {
			specifiedItem,
			specifiedRelativedItems
		} = getSpecifiedItems(
			canvasObj,
			currentNode.id || (currentNode[0] && currentNode[0].id)
		);
		this.setNextItemStatus(specifiedItem, specifiedRelativedItems, 2);
		// this.allNodes[currentNode.id] = {
		//   id: currentNode.id,
		//   status: 2,
		//   type: "node",
		// };
		// specifiedRelativedItems.forEach((item) => {
		//   this.allNodes[item.id] = {
		//     id: item.id,
		//     status: 2,
		//     type: "line",
		//   };
		// });
	}
	return currentNode;
};
// 根据 报警code和已保存的节点状态 完成节点状态初始化
function t_initNodeStatus(canvasObj, _, faultItem) {
	// 1.判断是否有保存的节点状态
	let processStatus =
		this.currentSelectedAlarm?.processStatus || faultItem?.processStatus;
	let armcode = this.currentSelectedAlarm?.armcode || faultItem?.armcode;
	if (processStatus && processStatus !== "{}") {
		let allNodes = null;
		try {
			allNodes = JSON.parse(processStatus);
		} catch (error) {
			allNodes = {};
			this.initStartNode(canvasObj);
		}
		if (allNodes && allNodes.currentOperationCode) {
			// 将三类节点进行处理
			// 1. 先处理操作节点
			// 2. 处理自动分析节点
			// 3. 处理tooltip节点
			// this.changeNodeStatusBySimpleData(canvasObj, allNodes);
		} else {
			allNodes &&
				Object.keys(allNodes).forEach((item) => {
					if (allNodes[item] && allNodes[item].status) {
						let tempNode = allNodes[item];
						this.allNodes[tempNode.id] = tempNode;
						if (tempNode.type == "line" && tempNode.status == 2) {
							this.linesOption.push(item.id);
						}
					}
				});
		}
		// // 获取 备选的连接线
		// Object.values(this.allNodes).forEach((item) => {
		//   if (item.type == "line" && item.status == 2) {
		//     this.linesOption.push(item.id);
		//   }
		// });
	} else {
		if (armcode) {
			// 1. 获取标识 code-e， 代表流程结束节点
			let currentNode = this.getNodeByCodeE(canvasObj, armcode);
			if (currentNode) {
				// 2. 获取所有标识 code-m， 代表流程中继节点
				let finishNodes = this.getNodeByCodeM(canvasObj, armcode);
				// 3. 获取所有需要改变的节点
				let nodeStatusNeededChanged = this.getNodeStatusNeededChanged(
					canvasObj,
					finishNodes,
					currentNode,
					armcode
				);
				// 4. 获取中继节点之间的连接线，并改变连线状态
				this.getLineStatusNeededChanged(canvasObj, nodeStatusNeededChanged);
			} else {
				this.initStartNode(canvasObj);
			}
		}
	}
};
// 获取初始节点
function initStartNode(canvasObj, status = 2) {
	// status 初始化节点的状态， 默认2 进行中
	let {
		specifiedItem,
		specifiedRelativedItems
	} = getSpecifiedItems(
		canvasObj,
		"lc0000"
	);
	let specifiedId =
		specifiedItem &&
		(specifiedItem.id || (specifiedItem[0] && specifiedItem[0].id));
		
	if (specifiedId) {
		this.allNodes[specifiedId] = {
			id: specifiedId,
			status: status,
			type: "node",
		};
	}
	if (status == 2) {
		specifiedRelativedItems &&
			specifiedRelativedItems.forEach((item) => {
				this.allNodes[item.id] = {
					id: item.id,
					status: 2,
					type: "line",
				};
			});
	}
	return {
		startNode: specifiedItem,
		startNodeRelativedLinkes: specifiedRelativedItems,
	};
};
// 将 连接线 变成初始状态
function t_initLink(canvasObj) {
	canvasObj.setValue({
		tag: "lcline",
		bkType: 0,
		locked: 10,
		// background: "grey",
		color: "#BFBFBFFF",
		toArrowColor: "grey",
		fromArrowColor: "grey",
		gradientFromColor: "#80808033",
		gradientToColor: "#80808099",
	});
};
// 将 节点 变成初始状态
function t_initNode(canvasObj) {
	canvasObj.setValue({
		tag: "lcnode",
		bkType: 2,
		locked: 10,
		background: "grey",
		color: "#BFBFBFFF",
		gradientFromColor: "#80808033",
		gradientToColor: "#80808099",
	});
};
// 初始化 节点状态
function initNodeStatus(canvasObj, initFuncs = [], faultItem) {

	let allinitFuncs = [
		"t_initLink",
		"t_initNode",
		"t_initNodeStatus",
		...initFuncs,
	];
	// canvasObj.render();

	this.linkLinesOption = canvasObj.find("lcline");
	allinitFuncs.forEach((item) => {
		this[item](canvasObj, initFuncs, faultItem);
	});
};
// 打开 流程图配置文件
function openProcessFile(canvasObj, processData) {
	canvasObj.open(processData); // 读文件
	canvasObj.lock(LockState.DisableEdit); // 只读
	canvasObj.fitView(true, 5); // 自适应
};
// 更新状态
function updateStatus(canvasObj) {
	renderStatus(canvasObj, this.allNodes, this.statusToColors);
};
// 处理其他连线， 将其置灰即实现未进行状态
function setOtherLineStatus(currentLine) {
	this.linesOption.length &&
		this.linesOption.forEach((item) => {
			if (item != currentLine.id) {
				this.allNodes[item] = {
					...this.allNodes[item],
					status: 0,
				};
			}
		});
	this.linesOption = [];
};
// 处理后续节点，将其设为进行中状态
function setNextItemStatus(nextNode, nextLines, status = 2) {
	let nextNodeId =
		(nextNode && (nextNode.id || (nextNode[0] && nextNode[0].id))) || "";
	this.allNodes[nextNodeId] = {
		...this.allNodes[nextNodeId],
		id: nextNodeId,
		type: "node",
		status: status,
	};
	nextLines.forEach((item) => {
		this.linesOption.push(item.id);
		this.allNodes[item.id] = {
			...this.allNodes[item.id],
			type: "line",
			id: item.id,
			status: status,
		};
	});
};
//双击线 处理后续连线状态为 0
// 处理后续节点，将其设为进行中状态
function setNextItemKongStatus(nextNode, nextLines, status = 0) {
	this.allNodes[nextNode.id] = {
		...this.allNodes[nextNode.id],
		id: nextNode.id,
		type: "node",
		status: status,
	};
	nextLines.forEach((item) => {
		this.linesOption.push(item.id);
		this.allNodes[item.id] = {
			...this.allNodes[item.id],
			type: "line",
			id: item.id,
			status: status,
		};
	});
};
// 处理前续节点，前续线段 将其设为进行中状态
function setPrevItemStatus(prevNode, prevLines) {
	this.allNodes[prevNode.id] = {
		...this.allNodes[prevNode.id],
		id: prevNode.id,
		type: "node",
		status: 0,
	};
	prevLines.forEach((item) => {
		this.linesOption.push(item.id);
		this.allNodes[item.id] = {
			...this.allNodes[item.id],
			type: "line",
			id: item.id,
			status: 2,
		};
	});
};